﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Net.Sockets;
using System.Net;
using Common;
using System.Threading.Tasks;

namespace ConsoleApplication1.Thread1._20121031
{
    public class myThreadStart
    {
        static void Main()
        {
            //设置CLR线程池最大线程数
            ThreadPool.SetMaxThreads(1000, 1000);
            float totalPrice = 0f;
            var parallelResult = Parallel.ForEach(GetOrderList(),
                       () => 0f,   //把参数初始值设为0
                       (order, state, orderPrice) =>
                       {
                           //计算单个Order的价格
                           orderPrice = GetOrderItem().Where(item => item.OrderID == order.ID)
                                .Sum(item => item.price * item.Count);
                           order.price = orderPrice;
                           ThreadPoolMessage(order);

                           return orderPrice;
                       },
                      (finallyPrice) =>
                      {
                          totalPrice += finallyPrice;//计算多个Order的总体价格
                      }
                  );

            while (!parallelResult.IsCompleted)
                Console.WriteLine("Doing Work!");

            Console.WriteLine("Total Price is:" + totalPrice);
            Console.ReadKey();
        }

        static IList<Order> GetOrderList()
         {
             IList<Order> orderList = new List<Order>();
             Order order1 = new Order();
             order1.ID = 1;
             orderList.Add(order1);

             Order order2 = new Order();
             order2.ID = 2;
             orderList.Add(order2);
             return orderList;
         }

        static IList<OrderItem> GetOrderItem()
         {
             IList<OrderItem> itemList = new List<OrderItem>();
 
             OrderItem orderItem1 = new OrderItem();
             orderItem1.ID = 1;
             orderItem1.Goods = "iPhone 4S";
             orderItem1.price = 6700;
             orderItem1.Count = 2;
             orderItem1.OrderID = 1;
             itemList.Add(orderItem1);

             OrderItem orderItem2 = new OrderItem();
             orderItem2.ID = 2;
             orderItem2.Goods = "iPhone 5";
             orderItem2.price = 8000;
             orderItem2.Count = 1;
             orderItem2.OrderID = 1;
             itemList.Add(orderItem2);

             OrderItem orderItem3 = new OrderItem();
             orderItem3.ID = 3;
             orderItem3.Goods = "iPad";
             orderItem3.price = 3000;
             orderItem3.Count = 1;
             orderItem3.OrderID = 2;
             itemList.Add(orderItem3);
             return itemList;
         }

        static void ThreadPoolMessage(Order order)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string message = string.Format("OrderID:{0}  OrderPrice:{1}\n" +
                   "  CurrentThreadId is {2}\n  WorkerThreads is:{3}" +
                   "  CompletionPortThreads is:{4}\n",
                   order.ID, order.price,
                   Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
            Console.WriteLine(message);
        }
    }
}
